package cn.run.java;

import cn.run.java.domain.Blog;
import cn.run.java.mapper.BlogMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

/**
 * 测试启动类
 * @author xiaoshitou
 */
public class Main {

  public static void main(String[] args) throws IOException {
    String resource = "mybatis-config.xml";
    // 获取配置文件
    InputStream inputStream = Resources.getResourceAsStream(resource);
    // 构建 SqlSessionFactory 会话工厂
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    // openSessionFromDataSource(configuration.getDefaultExecutorType(), null, false) 默认事务自动提交为false
    SqlSession sqlSession = sqlSessionFactory.openSession();
    // 获取 Mapper, Mapper 是由反射代理的对象
    BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
    // org.apache.ibatis.binding.MapperProxy 调用方法后会执行 这个的 invoke 方法
    mapper.selectAll().forEach(System.out::print);
    //    List<Blog> blogs = mapper.selectAll();
//    blogs.forEach(System.out::print);
    sqlSession.close();
  }

}
